package com.bianmaba.oauth2.authserver.web;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by xujingfeng on 2017/8/7.
 */
@RestController
public class ResourceController {

    @GetMapping("/user/me")
    public Object me(Principal principal) {
        Map<String, Object> map = new LinkedHashMap<String, Object>(3);
        map.put("name", principal.getName());
        if (principal instanceof OAuth2Authentication) {
            OAuth2Authentication oa = (OAuth2Authentication) principal;
            map.put("authorities", oa.getAuthorities());
            // map.put("userAuthentication", oa.getUserAuthentication());
            map.put("details", oa.getUserAuthentication().getDetails());
            return oa.getUserAuthentication();
        }
        return map;
    }
}
